View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.geronimo.ews.jaxrpcmapping;
17  
18  import org.apache.axis.constants.Scope;
19  import org.apache.axis.utils.CLArgsParser;
20  import org.apache.axis.utils.CLOption;
21  import org.apache.axis.utils.CLOptionDescriptor;
22  import org.apache.axis.utils.JavaUtils;
23  import org.apache.axis.utils.Messages;
24  import org.apache.axis.wsdl.gen.Parser;
25  import org.apache.axis.wsdl.gen.WSDL2;
26  
27  import java.util.List;
28  
29  /***
30   * @author Ias (iasandcb@tmax.co.kr)
31   */
32  public class WsdlToJ2ee extends WSDL2 {
33  
34      protected static final int MAPPING_OPT = 'm';
35  
36      // Define our short one-letter option identifiers.
37      protected static final int SERVER_OPT = 's';
38      protected static final int SKELETON_DEPLOY_OPT = 'S';
39      protected static final int NAMESPACE_OPT = 'N';
40      protected static final int NAMESPACE_FILE_OPT = 'f';
41      protected static final int OUTPUT_OPT = 'o';
42      protected static final int SCOPE_OPT = 'd';
43      protected static final int TEST_OPT = 't';
44      protected static final int PACKAGE_OPT = 'p';
45      protected static final int ALL_OPT = 'a';
46      protected static final int TYPEMAPPING_OPT = 'T';
47      protected static final int FACTORY_CLASS_OPT = 'F';
48      protected static final int HELPER_CLASS_OPT = 'H';
49      protected static final int USERNAME_OPT = 'U';
50      protected static final int PASSWORD_OPT = 'P';
51  
52      protected boolean bPackageOpt = false;
53      private J2eeEmitter emitter;
54  
55      /***
56       * Define the understood options. Each CLOptionDescriptor contains:
57       * - The "long" version of the option. Eg, "help" means that "--help" will
58       * be recognised.
59       * - The option flags, governing the option's argument(s).
60       * - The "short" version of the option. Eg, 'h' means that "-h" will be
61       * recognised.
62       * - A description of the option for the usage message
63       */
64      protected static final CLOptionDescriptor[] options = new CLOptionDescriptor[]{
65          new CLOptionDescriptor("server-side",
66                  CLOptionDescriptor.ARGUMENT_DISALLOWED,
67                  SERVER_OPT,
68                  Messages.getMessage("optionSkel00")),
69          new CLOptionDescriptor("skeletonDeploy",
70                  CLOptionDescriptor.ARGUMENT_REQUIRED,
71                  SKELETON_DEPLOY_OPT,
72                  Messages.getMessage("optionSkeletonDeploy00")),
73          new CLOptionDescriptor("NStoPkg",
74                  CLOptionDescriptor.DUPLICATES_ALLOWED + CLOptionDescriptor.ARGUMENTS_REQUIRED_2,
75                  NAMESPACE_OPT,
76                  Messages.getMessage("optionNStoPkg00")),
77          new CLOptionDescriptor("fileNStoPkg",
78                  CLOptionDescriptor.ARGUMENT_REQUIRED,
79                  NAMESPACE_FILE_OPT,
80                  Messages.getMessage("optionFileNStoPkg00")),
81          new CLOptionDescriptor("package",
82                  CLOptionDescriptor.ARGUMENT_REQUIRED,
83                  PACKAGE_OPT,
84                  Messages.getMessage("optionPackage00")),
85          new CLOptionDescriptor("output",
86                  CLOptionDescriptor.ARGUMENT_REQUIRED,
87                  OUTPUT_OPT,
88                  Messages.getMessage("optionOutput00")),
89          new CLOptionDescriptor("deployScope",
90                  CLOptionDescriptor.ARGUMENT_REQUIRED,
91                  SCOPE_OPT,
92                  Messages.getMessage("optionScope00")),
93          new CLOptionDescriptor("testCase",
94                  CLOptionDescriptor.ARGUMENT_DISALLOWED,
95                  TEST_OPT,
96                  Messages.getMessage("optionTest00")),
97          new CLOptionDescriptor("all",
98                  CLOptionDescriptor.ARGUMENT_DISALLOWED,
99                  ALL_OPT,
100                 Messages.getMessage("optionAll00")),
101         new CLOptionDescriptor("typeMappingVersion",
102                 CLOptionDescriptor.ARGUMENT_REQUIRED,
103                 TYPEMAPPING_OPT,
104                 Messages.getMessage("optionTypeMapping00")),
105         new CLOptionDescriptor("factory",
106                 CLOptionDescriptor.ARGUMENT_REQUIRED,
107                 FACTORY_CLASS_OPT,
108                 Messages.getMessage("optionFactory00")),
109         new CLOptionDescriptor("helperGen",
110                 CLOptionDescriptor.ARGUMENT_DISALLOWED,
111                 HELPER_CLASS_OPT,
112                 Messages.getMessage("optionHelper00")),
113         new CLOptionDescriptor("user",
114                 CLOptionDescriptor.ARGUMENT_REQUIRED,
115                 USERNAME_OPT,
116                 Messages.getMessage("optionUsername")),
117         new CLOptionDescriptor("password",
118                 CLOptionDescriptor.ARGUMENT_REQUIRED,
119                 PASSWORD_OPT,
120                 Messages.getMessage("optionPassword")),
121         new CLOptionDescriptor("mapping-file-path",
122                 CLOptionDescriptor.ARGUMENT_REQUIRED,
123                 MAPPING_OPT,
124                 "Choose a mapping file path for J2EE JAX-RPC mapping")
125     };
126 
127     public WsdlToJ2ee() {
128         emitter = (J2eeEmitter) parser;
129         addOptions(options);
130     } // ctor
131 
132     protected Parser createParser() {
133         return new J2eeEmitter();
134     } // createParser
135 
136     protected void parseOption(CLOption option) {
137         switch (option.getId()) {
138             case FACTORY_CLASS_OPT:
139                 emitter.setFactory(option.getArgument());
140                 break;
141             case HELPER_CLASS_OPT:
142                 emitter.setHelperWanted(true);
143                 break;
144             case SKELETON_DEPLOY_OPT:
145                 emitter.setSkeletonWanted(JavaUtils.isTrueExplicitly(option.getArgument(0)));
146                 // --skeletonDeploy assumes --server-side, so fall thru
147 
148             case SERVER_OPT:
149                 emitter.setServerSide(true);
150                 break;
151             case NAMESPACE_OPT:
152                 String namespace = option.getArgument(0);
153                 String packageName = option.getArgument(1);
154                 emitter.getNamespaceMap().put(namespace, packageName);
155                 break;
156             case NAMESPACE_FILE_OPT:
157                 emitter.setNStoPkg(option.getArgument());
158                 break;
159             case PACKAGE_OPT:
160                 bPackageOpt = true;
161                 emitter.setPackageName(option.getArgument());
162                 break;
163             case OUTPUT_OPT:
164                 emitter.setOutputDir(option.getArgument());
165                 break;
166             case SCOPE_OPT:
167                 String arg = option.getArgument();
168                 
169                 // Provide 'null' default, prevents logging internal error.
170                 // we have something different to report here.
171                 Scope scope = Scope.getScope(arg, null);
172                 if (scope != null) {
173                     emitter.setScope(scope);
174                 } else {
175                     System.err.println(Messages.getMessage("badScope00", arg));
176                 }
177                 break;
178             case TEST_OPT:
179                 emitter.setTestCaseWanted(true);
180                 break;
181             case ALL_OPT:
182                 emitter.setAllWanted(true);
183                 break;
184             case TYPEMAPPING_OPT:
185                 String tmValue = option.getArgument();
186                 if (tmValue.equals("1.1")) {
187                     emitter.setTypeMappingVersion("1.1");
188                 } else if (tmValue.equals("1.2")) {
189                     emitter.setTypeMappingVersion("1.2");
190                 } else {
191                     System.out.println(Messages.getMessage("badTypeMappingOption00"));
192                 }
193                 break;
194             case USERNAME_OPT:
195                 emitter.setUsername(option.getArgument());
196                 break;
197             case PASSWORD_OPT:
198                 emitter.setPassword(option.getArgument());
199                 break;
200             case MAPPING_OPT:
201                 emitter.setMappingFilePath(option.getArgument());
202                 break;
203             default:
204                 super.parseOption(option);
205         }
206     } // parseOption
207 
208     /***
209      * validateOptions
210      * This method is invoked after the options are set to validate
211      * the option settings.
212      */
213     protected void validateOptions() {
214         super.validateOptions();
215 
216         // validate argument combinations
217         if (emitter.isSkeletonWanted() && !emitter.isServerSide()) {
218             System.out.println(Messages.getMessage("badSkeleton00"));
219             printUsage();
220         }
221         if (!emitter.getNamespaceMap().isEmpty() && bPackageOpt) {
222             System.out.println(Messages.getMessage("badpackage00"));
223             printUsage();
224         }
225     } // validateOptions
226 
227     /***
228      * run
229      * checkes the command-line arguments and runs the tool.
230      *
231      * @param args String[] command-line arguments.
232      */
233     protected void run(String[] args) {
234         // Parse the arguments
235         CLArgsParser argsParser = new CLArgsParser(args, options);
236 
237         // Print parser errors, if any
238         if (null != argsParser.getErrorString()) {
239             System.err.println(Messages.getMessage("error01", argsParser.getErrorString()));
240             printUsage();
241         }
242 
243         // Get a list of parsed options
244         List clOptions = argsParser.getArguments();
245         int size = clOptions.size();
246 
247         // Parse the options and configure the emitter as appropriate.
248         for (int i = 0; i < size; i++) {
249             parseOption((CLOption) clOptions.get(i));
250         }
251 
252         // validate argument combinations
253         // 
254         validateOptions();
255         try {
256             parser.run(wsdlURI);
257         } catch (Exception e) {
258             e.printStackTrace();
259             System.exit(-1);
260         }
261     }    // run
262 
263     /***
264      * Main
265      */
266     public static void main(String args[]) {
267         WsdlToJ2ee j2ee = new WsdlToJ2ee();
268         j2ee.run(args);
269     } // main
270 }